//
//  DetailDetailViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/9.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "DetailDetailViewController.h"

@interface DetailDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) NSDictionary *dataDic;
@end

@implementation DetailDetailViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_tableView release];
    [_dataDic release];
    [_detailID release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"详情";
    [self dataHandler];
    
    [self createTableView];
    
}

#pragma mark - ------------ 数据处理 ---------------
- (void)dataHandler
{
    NSString *temp = @"http://www.duitang.com/napi/blog/detail/?__dtac=%257B%2522_r%2522%253A%2520%2522203659%2522%257D&app_code=gandalf&app_version=5.8%20rv%3A149591&blog_id=512642603&device_name=iPhone%205&device_platform=iPhone5%2C2&include_fields=tags%2Crelated_albums%2Crelated_albums.covers%2Croot_album%2Cshare_links_2%2Cextra_html%2Ctop_comments%2Ctop_like_users&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=568&screen_width=320&top_comments_count=12&top_forward_users_count=8&top_like_users_count=8";
    NSString *url = [temp stringByReplacingOccurrencesOfString:@"512642603" withString:_detailID];
    [XrDataParse GETWithURL:url par:nil success:^(id responseObject) {
        
        self.dataDic = responseObject[@"data"];

        [_tableView reloadData];
        
    } filed:^(NSError *error) {
        
    }];

}


#pragma mark - ------------ 创建Tableview ---------------
- (void)createTableView
{

    self.tableView = [[BaseTableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[DetailTableViewCell class] forCellReuseIdentifier:@"pool"];
/** 夜间 */
    _tableView.dk_backgroundColorPicker = DKColorWithColors(GrayLight ,[UIColor lightGrayColor]);
    
    [_tableView release];
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}

#pragma mark - ------------ cell赋值 ---------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    cell.dataDic = _dataDic;
    
    /** 取消选中 */
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //设置tableView隐藏分隔线
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    cell.dk_backgroundColorPicker = DKColorWithColors(GrayLight ,[UIColor lightGrayColor]);


    
    return cell;
}

#pragma mark - ------------ 返回高度 ---------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataDic) {

        //返回图片高 + 文字高 + 其他控件
        CGFloat photo = [DetailTableViewCell heightWithPhoto:_dataDic[@"photo"][@"width"]  height:_dataDic[@"photo"][@"height"] ];
        
        CGFloat text = [DetailTableViewCell heightWithText:_dataDic[@"msg"]];
        
        return  photo + text + 111 ;
    }
    return 0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
